﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using HotelManageSystem.Domain;
using HotelManageSystem.Domain.Entities;
using HotelManageSystem.Domain.Infrastructure;
using HotelManageSystem.Models;
using Telerik.Web.Mvc;

namespace HotelManageSystem.Controllers
{
    public class ProductProviderController : Controller
    {
        //
        // GET: /ProductProvider/
        private readonly IProductProviderRepository _productProviderRepository;
        private readonly IUnitOfWork _unitOfWork;
        public ProductProviderController(IUnitOfWork unitOfWork,IProductProviderRepository productProviderRepository)
        {
            _productProviderRepository = productProviderRepository;
            _unitOfWork = unitOfWork;
        }
        
        private IEnumerable<ProductProviderModel> GetproductProviderModels()
        {
            return from p in _productProviderRepository.GetAll().AsQueryable()
                   select new ProductProviderModel
                              {
                                  ID = p.ID,
                                  Name = p.Name,
                                  Address = p.Address,
                                  Tel = p.Tel,
                              };
        }

        public ActionResult Index()
        {
            return View(GetproductProviderModels());
        }
        [GridAction]
        public ActionResult TelerikAjax()
        {
            return View(new GridModel(GetproductProviderModels()));
        }
        [HttpPost]
        [GridAction]
        public ActionResult Insert()
        {
            var productProvider = new ProductProvider();
            if(TryUpdateModel(productProvider))
            {
                _productProviderRepository.Add(productProvider);
                _unitOfWork.Commit();
            }
            return View(new GridModel(GetproductProviderModels()));
        }
        [HttpPost]
        [GridAction]
        public ActionResult Update(int id)
        {
            var productprovider = _productProviderRepository.GetById(id);
            if(TryUpdateModel(productprovider))
            {
                _productProviderRepository.Update(productprovider);
                _unitOfWork.Commit();
            }
            return View(new GridModel(GetproductProviderModels()));
        }
        [HttpPost]
        [GridAction]
        public ActionResult Delete(int id)
        {
            var productprovider = _productProviderRepository.GetById(id);
            if(TryUpdateModel(productprovider))
            {
                _productProviderRepository.Delete(productprovider);
                _unitOfWork.Commit();
            }
            return View(new GridModel(GetproductProviderModels()));
        }
    }
}
